/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package byzantine;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author rpathan
 */
public class General {

    private Integer ID;
    private String type;
    private Map<Integer, ArrayList<Message>> sentMessages;
    private Map<Integer, ArrayList<Message>> receivedMessages;
    private Map<Integer, ArrayList<Message>> decisionMessages;

    public General(Integer ID, String type) {
        this.ID = ID;
        this.type = type;
        sentMessages = new HashMap<Integer, ArrayList<Message>>();
        receivedMessages = new HashMap<Integer, ArrayList<Message>>();
        decisionMessages = new HashMap<Integer, ArrayList<Message>>();
    }

    public Integer getID() {
        return ID;
    }

    public String getType() {
        return type;
    }

    public void sendMessage(Integer round, Message message, General general) {
        this.addSentMessage(round, message);
        general.addReceivedMessage(round, message);
    }

    public void addSentMessage(Integer round, Message message) {

        if (sentMessages.containsKey(round)) {
            sentMessages.get(round).add(message);
        } else {
            ArrayList<Message> roundMessages = new ArrayList<Message>();
            roundMessages.add(message);
            sentMessages.put(round, roundMessages);
        }
    }

    public ArrayList<Message> getSentMessages(Integer round) {

        return sentMessages.get(round);
    }

    public void addReceivedMessage(Integer round, Message message) {

        if (receivedMessages.containsKey(round)) {
            receivedMessages.get(round).add(message);
        } else {
            ArrayList<Message> roundMessages = new ArrayList<Message>();
            roundMessages.add(message);
            receivedMessages.put(round, roundMessages);
        }
    }

    public ArrayList<Message> getReceivedMessages(Integer round) {

        return receivedMessages.get(round);
    }

    public void addDecisionMessage(Integer round, Message message) {

        if (decisionMessages.containsKey(round)) {
            decisionMessages.get(round).add(message);
        } else {
            ArrayList<Message> roundMessages = new ArrayList<Message>();
            roundMessages.add(message);
            decisionMessages.put(round, roundMessages);
        }
    }

    public ArrayList<Message> getDecisionMessages(Integer round) {

        return decisionMessages.get(round);
    }

    @Override
    public String toString() {
        return ID.toString() + " " + type;
    }
}
